package com.cqs.learning.spring.cloud.producer.config;

import com.alibaba.csp.sentinel.datasource.Converter;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.redis.RedisDataSource;
import com.alibaba.csp.sentinel.datasource.redis.config.RedisConnectionConfig;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

//@Configuration
//@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfig {


    @Resource(name = "redisConnectionConfig")
    private RedisConnectionConfig redisConnectionConfig;

    @Bean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisCacheTemplate(LettuceConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }


//    @Bean
//    public RedisMessageListenerContainer redisMessageListenerContainer(LettuceConnectionFactory connectionFactory) {
//        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
//        container.setConnectionFactory(connectionFactory);
//        container.addMessageListener(new MessageListenerAdapter(redisConsumer,"consume"), new ChannelTopic(SENTINEL_FLOW_RULES_CHANNEL));
//        return container;
//    }

    @Bean(name = "redisConnectionConfig")
    public RedisConnectionConfig redisConnectionConfig() {
        RedisConnectionConfig config = RedisConnectionConfig.builder()
                //cqs.redis.com:26379,cqs.redis.com:26380,cqs.redis.com:26381
                .withRedisSentinel("cqs.redis.com", 26379)
                .withRedisSentinel("cqs.redis.com", 26380)
                .withRedisSentinel("cqs.redis.com", 26381)
                .withSentinelMasterId("mymaster").build();
        return config;
    }


    /**
     * redis 管理限流熔断规则
     * push 模式
     */
//    @PostConstruct
    public void flowRuleListParser() {
        Converter<String, List<FlowRule>> flowRuleListParser = source -> JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
        });
        //这里实现了注册得功能
        ReadableDataSource<String, List<FlowRule>> redisDataSource = new RedisDataSource<>(redisConnectionConfig,
                Contants.SENTINEL_FLOW_RULES_CHANNEL_KEY, Contants.SENTINEL_FLOW_RULES_CHANNEL, flowRuleListParser);
        //限流规则 -- 全量覆盖更新规则()
        FlowRuleManager.register2Property(redisDataSource.getProperty());

        //熔断规则
        Converter<String, List<DegradeRule>> degreRuleListParser = source -> JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
        });
        ReadableDataSource<String, List<DegradeRule>> redisDataSource2 = new RedisDataSource<>(redisConnectionConfig,
                Contants.SENTINEL_DEGRADE_RULES_CHANNEL_KEY, Contants.SENTINEL_DEGRADE_RULES_CHANNEL, degreRuleListParser);
        DegradeRuleManager.register2Property(redisDataSource2.getProperty());

    }


}